package it.gestoreContratti.inputVerifiers;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class StringInputVerifier extends InputVerifier {

	private String field;
	private int length;
	
	public StringInputVerifier(String field, int len) {
		this.field = field;
		this.length = len;		
	}
	@Override
	public boolean verify(JComponent input) {
		
		if(input instanceof JTextField){
			JTextField textField = (JTextField) input;
			if(textField.getText().length() > length){
				JOptionPane.showMessageDialog(null, "Il campo "+field+" deve essere lungo al massimo "+length+" caratteri", "", JOptionPane.WARNING_MESSAGE);
				textField.setText("");
				textField.validate();
				return false;			
			}else{
				return true;
			}		
		} else{
			JTextArea textArea = (JTextArea) input;
			if(textArea.getText().length() > length){
				JOptionPane.showMessageDialog(null, "Il campo "+field+" deve essere lungo al massimo "+length+" caratteri", "", JOptionPane.WARNING_MESSAGE);
				textArea.setText("");
				textArea.validate();
				return false;			
			}else{
				return true;
			}		
		}
		
	}

}
